<template>
  <div class="page-container">
    <button class="back-button" @click="goToHome">
      <span class="icon-arrow-left"></span>
      返回首页
    </button>
    
    <div class="page-header">
      <h1>团队管理</h1>
      <p>管理公司所有团队信息，包括创建、编辑和成员管理</p>
    </div>

    <div class="action-bar">
      <button class="primary-btn" @click="showAddTeamModal = true">
        <i class="icon-plus"></i> 新建团队
      </button>
      <div class="search-box">
        <input type="text" v-model="searchKeyword" placeholder="搜索团队...">
        <button class="search-btn"><i class="icon-search"></i></button>
      </div>
    </div>

    <div class="team-list">
      <div class="team-card" v-for="team in filteredTeams" :key="team.id">
        <div class="team-header">
          <h3>{{ team.name }}</h3>
          <div class="team-actions">
            <button class="icon-btn" @click="editTeam(team)"><i class="icon-edit"></i></button>
            <button class="icon-btn danger" @click="deleteTeam(team.id)"><i class="icon-trash"></i></button>
          </div>
        </div>
        <div class="team-info">
          <p><strong>负责人:</strong> {{ team.leader }}</p>
          <p><strong>成员数:</strong> {{ team.memberCount }}</p>
          <p><strong>创建时间:</strong> {{ team.createTime }}</p>
          <p><strong>描述:</strong> {{ team.description }}</p>
        </div>
        <div class="team-footer">
          <button class="secondary-btn" @click="manageMembers(team.id)">管理成员</button>
        </div>
      </div>
      
      <div class="empty-state" v-if="filteredTeams.length === 0">
        <i class="icon-team"></i>
        <p>暂无团队数据</p>
        <button class="primary-btn" @click="showAddTeamModal = true">创建第一个团队</button>
      </div>
    </div>

    <!-- 添加/编辑团队弹窗 -->
    <div class="modal-backdrop" v-if="showAddTeamModal">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ isEditing ? '编辑团队' : '新建团队' }}</h3>
          <button class="close-btn" @click="showAddTeamModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="saveTeam">
            <div class="form-group">
              <label>团队名称 *</label>
              <input type="text" v-model="currentTeam.name" required>
            </div>
            <div class="form-group">
              <label>团队负责人 *</label>
              <input type="text" v-model="currentTeam.leader" required>
            </div>
            <div class="form-group">
              <label>团队描述</label>
              <textarea v-model="currentTeam.description" rows="3"></textarea>
            </div>
            <div class="modal-footer">
              <button type="button" class="secondary-btn" @click="showAddTeamModal = false">取消</button>
              <button type="submit" class="primary-btn">保存</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TeamManage',
  data() {
    return {
      searchKeyword: '',
      showAddTeamModal: false,
      isEditing: false,
      currentTeam: {
        id: null,
        name: '',
        leader: '',
        description: ''
      },
      teams: [
        {
          id: 1,
          name: '前端开发团队',
          leader: '张三',
          memberCount: 8,
          createTime: '2023-01-15',
          description: '负责公司所有产品的前端开发工作'
        },
        {
          id: 2,
          name: '后端开发团队',
          leader: '李四',
          memberCount: 12,
          createTime: '2023-01-15',
          description: '负责公司所有产品的后端开发和API设计'
        },
        {
          id: 3,
          name: '测试团队',
          leader: '王五',
          memberCount: 6,
          createTime: '2023-02-20',
          description: '负责产品的功能测试和性能测试'
        }
      ]
    };
  },
  computed: {
    filteredTeams() {
      if (!this.searchKeyword) return this.teams;
      const keyword = this.searchKeyword.toLowerCase();
      return this.teams.filter(team => 
        team.name.toLowerCase().includes(keyword) || 
        team.leader.toLowerCase().includes(keyword) ||
        team.description.toLowerCase().includes(keyword)
      );
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    editTeam(team) {
      this.isEditing = true;
      this.currentTeam = { ...team };
      this.showAddTeamModal = true;
    },
    deleteTeam(id) {
      if (confirm('确定要删除这个团队吗？')) {
        this.teams = this.teams.filter(team => team.id !== id);
      }
    },
    manageMembers(teamId) {
      // 可以跳转到团队成员管理页面
      this.$router.push(`/team-members/${teamId}`);
    },
    saveTeam() {
      if (this.isEditing) {
        const index = this.teams.findIndex(t => t.id === this.currentTeam.id);
        this.teams.splice(index, 1, this.currentTeam);
      } else {
        this.currentTeam.id = Date.now();
        this.currentTeam.createTime = new Date().toLocaleDateString();
        this.currentTeam.memberCount = 0;
        this.teams.push(this.currentTeam);
      }
      this.resetModal();
    },
    resetModal() {
      this.showAddTeamModal = false;
      this.isEditing = false;
      this.currentTeam = {
        id: null,
        name: '',
        leader: '',
        description: ''
      };
    }
  }
};
</script>

<style scoped>
.page-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin: 20px 0 30px;
}

.page-header h1 {
  color: #2c3e50;
  margin-bottom: 8px;
}

.page-header p {
  color: #7f8c8d;
  font-size: 14px;
}

.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 15px;
}

.primary-btn {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
}

.primary-btn:hover {
  background: #2980b9;
  transform: translateY(-1px);
}

.secondary-btn {
  background: #ecf0f1;
  color: #2c3e50;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  cursor: pointer;
  transition: all 0.2s;
}

.secondary-btn:hover {
  background: #dcdde1;
}

.search-box {
  display: flex;
  align-items: center;
}

.search-box input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
  outline: none;
  width: 250px;
}

.search-box input:focus {
  border-color: #3498db;
}

.search-btn {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  padding: 8px 12px;
  cursor: pointer;
}

.team-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.team-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
}

.team-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.team-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
}

.team-info {
  padding: 15px 20px;
}

.team-info p {
  margin: 8px 0;
  font-size: 14px;
  color: #555;
}

.team-footer {
  padding: 12px 20px;
  background: #f8f9fa;
  border-top: 1px solid #eee;
  text-align: right;
}

.icon-btn {
  background: none;
  border: none;
  color: #7f8c8d;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s;
}

.icon-btn:hover {
  color: #3498db;
  background: #f1f1f1;
}

.icon-btn.danger:hover {
  color: #e74c3c;
}

.empty-state {
  text-align: center;
  padding: 50px 20px;
  color: #7f8c8d;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 15px;
  color: #bdc3c7;
}

/* 弹窗样式 */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  color: #2c3e50;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.form-group textarea {
  resize: vertical;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #7f8c8d;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

/* 图标样式模拟 */
.icon-plus::before { content: "+"; }
.icon-search::before { content: "🔍"; }
.icon-edit::before { content: "✏️"; }
.icon-trash::before { content: "🗑️"; }
.icon-arrow-left::before { content: "←"; }
.icon-team::before { content: "👥"; }
</style>